#include "testactor.h"
#include <OgreEntity.h>
#include <stdio.h>
#include <OgreMath.h>
#include <OgreVector3.h>
#include "OgreParticleSystem.h"
#include <OgreParticleEmitter.h>

#include "../weapon/pfire.h"
#include "../weapon/pspread.h"

TestActor::TestActor(World *world): BaseEnemy(world){
    createModel();

    m_node->scale(10,10,10);
    m_speed = rand() % 200 + 60;
    m_lastShoot = m_world->getCurrentTime();
    m_creationTime = m_world->getCurrentTime();
    m_health = 1;
}

void TestActor::move(float time){
    Ogre::Vector3 diff = m_world->getPlayer()->getPosition() - m_node->getPosition();

    float elapsed = m_world->getCurrentTime() - m_creationTime;

    m_node->lookAt(m_world->getPlayer()->getPosition(), Ogre::Node::TS_PARENT, Ogre::Vector3::UNIT_Z);

    if(m_world->getCurrentTime() - m_lastShoot > 500){
        Projectile *projectile= new PSpread(m_world, 1000, 100, -diff.getRotationTo(Ogre::Vector3(0,0,1)).getYaw().valueDegrees());
        m_world->addActor(projectile, m_node->getPosition());
        m_lastShoot = m_world->getCurrentTime();
    }

    for(int i = 0; i < m_world->getActorsCount();i++){
        AI* actor = m_world->getActor(i);
        if(actor->getType() == getType() && actor != this){
            Ogre::Vector3 actorDiff = m_node->getPosition() - actor->getPosition();
            float distance = actorDiff.length();
            actorDiff.normalise();
            diff += 1000/distance * actorDiff;
        }
    }

    diff.normalise();
    diff *= m_speed;

    if(elapsed <= 15000){
        m_node->setPosition(m_node->getPosition() + time* diff);
    }
    else{
        if(this->getPosition().x <= 0){
            Ogre::Vector3 awayRight = Ogre::Vector3(-100,0,100);
            m_node->setPosition(m_node->getPosition() + time* awayRight);
        }
        else{
            Ogre::Vector3 awayLeft = Ogre::Vector3(100,0,100);
            m_node->setPosition(m_node->getPosition() + time* awayLeft);
        }
    }
}

void TestActor::colide(AI *object){
    if((object->getType() == TYPE_BULLET || object->getType() == TYPE_FLAMER) && !object->isNpc()){
        m_health -= object->getDamage();
        if(m_health <= 0){
            m_logger->m_score += 2;

            std::stringstream logInfo;
            logInfo << "score update, score value," << m_logger->m_score << ", player shot hunter vessel";
            m_logger->log(logInfo.str());

            m_world->setScore(m_logger->m_score);
            m_world->removeActor(this);
            this->animDeath(this->getPosition());
            m_world->removeActor(this);
        }
    }
}

void TestActor::animDeath(Ogre::Vector3 lastPos){
    Ogre::SceneManager *scene = m_world->getSceneManager();

    // create particle system and attach it to d_node at death position
    Ogre::ParticleSystem* splatter = scene->createParticleSystem(25);
    splatter->setMaterialName("Examples/Flare");
    splatter->setDefaultDimensions(30, 30);

    Ogre::ParticleEmitter* emitter = splatter ->addEmitter("Point");  // add a point emitter

    // set the emitter properties
    emitter->setAngle(Ogre::Degree(180));
    emitter->setMinTimeToLive(0.5);
    emitter->setMaxTimeToLive(2);
    emitter->setDuration(0.1);
    emitter->setEmissionRate(1000);
    emitter->setParticleVelocity(200);
    emitter->setDirection(Ogre::Vector3::NEGATIVE_UNIT_Z);
    emitter->setColour(Ogre::ColourValue::White);

    Ogre::SceneNode *d_node = scene->getRootSceneNode()->createChildSceneNode();
    d_node->setPosition(lastPos);
    d_node->attachObject(splatter);
}
